-- RustyBow
-- Created by chengb Feb/3/2016
-- 生锈的重弩格子

-- 初始化
function initRustyBow(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function okFunc()
        self.item:setVisible(false);

        local tip = buildingInfo["bonus_tip"];
        if type(tip) == "string" and tip ~= "" then
            alert(tip);
        end

        -- 播放生锈的重弩消失特效
        playMonsterFleeEffect(self);

        local function transform()
            local pos = self.gridData:getPos();
            DungeonActionM.go("install_bow", pos);
        end

        performWithDelay(self, transform, 0.01);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 打开建筑查看界面
            self:openBuildingView(okFunc);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end


